<?php

namespace app\common\model;

use app\common\model\base\BaseModel;
use app\common\traits\InstanceTrait;

class Plan extends BaseModel
{
    use InstanceTrait;

    /**
     * 状态配置
     */
    const STATE_UNDONE = 1;
    const STATE_DONE = 2;
    const STATE_MAPS = [
        self::STATE_UNDONE => '未完成',
        self::STATE_DONE => '已完成',
    ];

    public function __construct($data = [], Model $parent = NULL, $table = '')
    {
        parent::__construct($data, $parent, $table);
        $this->pk = 'plan_id';
        $this->initBaseField();
    }

    /**
     * 基础的where,所有查询都应以此为查询条件
     * @param array $param
     * @return mixed
     */
    public static function getParamWhere($param = [], $alias = '')
    {
        if (!empty($alias)) {
            $alias = strpos($alias, '.') === FALSE ? $alias . "." : $alias;
        }
        $where = parent::getBaseWhere($param, $alias);
        if (!empty($params['plan_id'])) {
            if (is_array($params['plan_id'])) {
                $where[] = [$alias . 'plan_id', 'in', $params['plan_id']];
            } else {
                $where[] = [$alias . 'plan_id', '=', $params['plan_id']];
            }
        }
        if (!empty($params['content'])) {
            if (is_array($params['content'])) {
                $where[] = [$alias . 'content', 'in', $params['content']];
            } else {
                $where[] = [$alias . 'content', 'like', "%{$params['content']}%"];
            }
        }
        if (!empty($params['plan_time'])) {
            if (is_array($params['plan_time'])) {
                $where[] = [$alias . 'plan_time', 'in', $params['plan_time']];
            } else {
                $where[] = [$alias . 'plan_time', '=', $params['plan_time']];
            }
        }
        if (!empty($params['plan_time_start'])) {
            $where[] = [$alias . 'plan_time', '>=', $params['plan_time_start']];
        }
        if (!empty($params['plan_time_end'])) {
            $where[] = [$alias . 'plan_time', '<=', $params['plan_time_end']];
        }
        if (!empty($params['create_time'])) {
            if (is_array($params['create_time'])) {
                $where[] = [$alias . 'create_time', 'in', $params['create_time']];
            } else {
                $where[] = [$alias . 'create_time', '=', $params['create_time']];
            }
        }
        if (!empty($params['create_time_start'])) {
            $where[] = [$alias . 'create_time', '>=', $params['create_time_start']];
        }
        if (!empty($params['create_time_end'])) {
            $where[] = [$alias . 'create_time', '<=', $params['create_time_end']];
        }
        if (!empty($params['update_time'])) {
            if (is_array($params['update_time'])) {
                $where[] = [$alias . 'update_time', 'in', $params['update_time']];
            } else {
                $where[] = [$alias . 'update_time', '=', $params['update_time']];
            }
        }
        if (!empty($params['update_time_start'])) {
            $where[] = [$alias . 'update_time', '>=', $params['update_time_start']];
        }
        if (!empty($params['update_time_end'])) {
            $where[] = [$alias . 'update_time', '<=', $params['update_time_end']];
        }
        if (!empty($params['finish_time'])) {
            if (is_array($params['finish_time'])) {
                $where[] = [$alias . 'finish_time', 'in', $params['finish_time']];
            } else {
                $where[] = [$alias . 'finish_time', '=', $params['finish_time']];
            }
        }
        if (!empty($params['finish_time_start'])) {
            $where[] = [$alias . 'finish_time', '>=', $params['finish_time_start']];
        }
        if (!empty($params['finish_time_end'])) {
            $where[] = [$alias . 'finish_time', '<=', $params['finish_time_end']];
        }
        if (!empty($params['cycle'])) {
            if (is_array($params['cycle'])) {
                $where[] = [$alias . 'cycle', 'in', $params['cycle']];
            } else {
                $where[] = [$alias . 'cycle', '=', $params['cycle']];
            }
        }
        if (!empty($params['member_id'])) {
            if (is_array($params['member_id'])) {
                $where[] = [$alias . 'member_id', 'in', $params['member_id']];
            } else {
                $where[] = [$alias . 'member_id', '=', $params['member_id']];
            }
        }
        if (!empty($params['status'])) {
            if (is_array($params['status'])) {
                $where[] = [$alias . 'status', 'in', $params['status']];
            } else {
                $where[] = [$alias . 'status', '=', $params['status']];
            }
        }
        if (!empty($params['delete_time'])) {
            if (is_array($params['delete_time'])) {
                $where[] = [$alias . 'delete_time', 'in', $params['delete_time']];
            } else {
                $where[] = [$alias . 'delete_time', '=', $params['delete_time']];
            }
        }
        if (!empty($params['delete_time_start'])) {
            $where[] = [$alias . 'delete_time', '>=', $params['delete_time_start']];
        }
        if (!empty($params['delete_time_end'])) {
            $where[] = [$alias . 'delete_time', '<=', $params['delete_time_end']];
        }
        if (!empty($params['creater_id'])) {
            if (is_array($params['creater_id'])) {
                $where[] = [$alias . 'creater_id', 'in', $params['creater_id']];
            } else {
                $where[] = [$alias . 'creater_id', '=', $params['creater_id']];
            }
        }
        if (!empty($params['updater_id'])) {
            if (is_array($params['updater_id'])) {
                $where[] = [$alias . 'updater_id', 'in', $params['updater_id']];
            } else {
                $where[] = [$alias . 'updater_id', '=', $params['updater_id']];
            }
        }
        if (!empty($params['updater_id_start'])) {
            $where[] = [$alias . 'updater_id', '>=', $params['updater_id_start']];
        }
        if (!empty($params['updater_id_end'])) {
            $where[] = [$alias . 'updater_id', '<=', $params['updater_id_end']];
        }
        if (!empty($params['pid'])) {
            if (is_array($params['pid'])) {
                $where[] = [$alias . 'pid', 'in', $params['pid']];
            } else {
                $where[] = [$alias . 'pid', '=', $params['pid']];
            }
        }
        if (!empty($params['state'])) {
            if (is_array($params['state'])) {
                $where[] = [$alias . 'state', 'in', $params['state']];
            } else {
                $where[] = [$alias . 'state', '=', $params['state']];
            }
        }
        return $where;
    }

    public static function getTheList($params, $field = '*')
    {
        $where = static::getParamWhere($params);
        $param['page'] = !empty($param['page']) ? $param['page'] : 1;
        $param['limit'] = !empty($param['limit']) ? $param['limit'] : 1000;
        $data = static::where($where)
                      ->field($field)
                      ->page($param['page'], $param['limit'])
                      ->select();
        return $data;
    }
}
